package com.audaque.springboot.foshanupload.web.productdemo.doc;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * @author zhongguobin
 * @date 2024-03-24 15:15
 * @description TODO
 */
@Slf4j
@Data
@Getter
@Setter
@Accessors(chain = true)
@Document(indexName = "product", shards = 1, replicas = 1)
@Schema(name = "ProductEs", description = "产品")
public class ProductEs implements Serializable {
    private static final long serialVersionUID = 1L;


    //使用 @Id 注解的字段作为文档的 _id 值
    @Id
    @Schema(description = "id")
    @Field(value = "id", type = FieldType.Long)
    private Long id;


    @Field(type = FieldType.Text, analyzer = "standard")
    @Schema(description = "sku")
    private String sku;

    @Field(type = FieldType.Text, analyzer = "standard")
    @Schema(description = "标题")
    private String title;

    @Field(type = FieldType.Text, analyzer = "standard")
    @Schema(description = "描述")
    private String description;

    public static String getDocName() {
        String s = ProductEs.class.getAnnotation(Document.class).indexName();
        return s;
    }

    public static String[] getDocStringFieldName() {
        java.lang.reflect.Field[] fields = ProductEs.class.getDeclaredFields();
        Stream<java.lang.reflect.Field> stream = Arrays.stream(fields);
        String[] array = stream.filter(e -> {
                    String typeName = e.getType().getTypeName();
                    log.debug("typeName:" + typeName);
                    return Objects.equals(typeName, "java.lang.String");
                }

        ).map(f -> f.getName()).toArray(String[]::new);
        return array;
    }


}
